<?php
namespace app\shell;

use app\service\UserDeviceBindingService;
use app\service\UserDeviceShareService;
use share\console\CConsole;
use share\console\CLIRunner;
use share\service\cache\UserInfoAccountCacheService;
use share\service\cache\UserInfoCacheService;
use share\service\UserBaseService;

/**
 * 
 */
class CRepairAccount extends CLIRunner{

    public function repair_mutil_account_action(){
        $phoneAccount = self::get_error_phone_account_info();
        $this->del_empty_identity_account($phoneAccount,'phone');

        $emailAccount = self::get_error_email_account_info();
        $this->del_empty_identity_account($emailAccount,'email');

        return array_merge($phoneAccount,$emailAccount);
    }

    public function del_empty_identity_account($accountInfo,$key = 'phone'){
        foreach($accountInfo as $account){
            UserBaseService::delByFilter([
                $key => $account[$key],
                'vendor' => $account['vendor'],
                'identity_id' => ''
            ]);
        }
    }

    /**
     * 获取有问题的账号数据
     */
    public static function get_error_phone_account_info(){
        $model = UserBaseService::getModel();
        $qeury = $model->getQuery(true);
        return $qeury->query("SELECT `phone`,`vendor` FROM `user_info` where `phone` <> '' GROUP BY `phone`,`vendor` HAVING count(`user_id`) > 1");
    }

    public static function get_error_email_account_info(){
        $model = UserBaseService::getModel();
        $qeury = $model->getQuery(true);
        return $qeury->query("SELECT `email`,`vendor` FROM `user_info` where `email` <> '' GROUP BY `email`,`vendor` HAVING count(`user_id`) > 1");
    }

    /**
     * 删除非默认登录账号 且 没有绑定和分享信息的账号
     */
    public static function analysis_error_account_action(){
        $phoneAccount = self::get_error_phone_account_info();
        foreach($phoneAccount as $account){
            CConsole::print_line("test account phone:".$account['phone']." vendor:".$account['vendor']);
            $allUser = UserBaseService::getAllByFilter([
                'phone' => $account['phone'],
                'vendor' => $account['vendor'],
            ]);
            $loginUser = UserBaseService::getByPhone($account['phone'],[             
                'vendor' => $account['vendor'],
            ]);
            CConsole::print_line("default login user_id is ".$loginUser['user_id']);
            foreach($allUser as $user){
                $bindUser = UserDeviceBindingService::getByFilter(['user_id' => $user['user_id']]);
                $shareUser = UserDeviceShareService::getByFilter(['share_id' => $user['user_id']]);
                if($bindUser){
                    CConsole::print_line("user_id is ".$user['user_id']." has bind device");
                }
                else{
                    CConsole::print_line("user_id is ".$user['user_id']." not has bind device");
                }

                if($shareUser){
                    CConsole::print_line("user_id is ".$user['user_id']." has share device");
                }
                else{
                    CConsole::print_line("user_id is ".$user['user_id']." not has share device");
                }

                // 啥都没有的就删掉
                if($user['user_id'] !== $loginUser['user_id'] && empty($bindUser) && empty($shareUser)){
                    CConsole::print_line("now to delete user ".$user['user_id']);
                    UserBaseService::del($user['user_id']);
                } 
            }            
        }

        $emailAccount = self::get_error_email_account_info();
        foreach($emailAccount as $account){
            CConsole::print_line("test account email:".$account['email']." vendor:".$account['vendor']);
            $allUser = UserBaseService::getAllByFilter([
                'email' => $account['email'],
                'vendor' => $account['vendor'],
            ]);
            $loginUser = UserBaseService::getByEmail($account['email'],[             
                'vendor' => $account['vendor'],
            ]);
            CConsole::print_line("default login user_id is ".$loginUser['user_id']);
            foreach($allUser as $user){
                $bindUser = UserDeviceBindingService::getByFilter(['user_id' => $user['user_id']]);
                $shareUser = UserDeviceShareService::getByFilter(['share_id' => $user['user_id']]);
                if($bindUser){
                    CConsole::print_line("user_id is ".$user['user_id']." has bind device");
                }
                else{
                    CConsole::print_line("user_id is ".$user['user_id']." not has bind device");
                }

                if($shareUser){
                    CConsole::print_line("user_id is ".$user['user_id']." has share device");
                }
                else{
                    CConsole::print_line("user_id is ".$user['user_id']." not has share device");
                }

                // 啥都没有的就删掉
                if($user['user_id'] !== $loginUser['user_id'] && empty($bindUser) && empty($shareUser)){
                    CConsole::print_line("now to delete user ".$user['user_id']);
                    UserBaseService::del($user['user_id']);
                } 
            }            
        }
    }

    /**
     * 备份账号不是默认登录账号的数据 / 通过修改厂商
     */
    public static function backup_error_account_action(){
        $phoneAccount = self::get_error_phone_account_info();
        foreach($phoneAccount as $account){
            CConsole::print_line("test account phone:".$account['phone']." vendor:".$account['vendor']);
            $allUser = UserBaseService::getAllByFilter([
                'phone' => $account['phone'],
                'vendor' => $account['vendor'],
            ]);
            $loginUser = UserBaseService::getByPhone($account['phone'],[             
                'vendor' => $account['vendor'],
            ]);
            CConsole::print_line("default login user_id is ".$loginUser['user_id']);
            foreach($allUser as $user){
                if($user['user_id'] !== $loginUser['user_id']){
                    $vendor = $user['vendor'] . date("ymdhis").mt_rand(1000,9999);
                    CConsole::print_line("default set user_id ".$user['user_id']." vendor as ".$vendor);
                    UserBaseService::update($user['user_id'],[
                        'vendor' => $vendor 
                    ]);
                }
            }            
        }
        $emailAccount = self::get_error_email_account_info();
        foreach($emailAccount as $account){
            CConsole::print_line("test account email:".$account['email']." vendor:".$account['vendor']);
            $allUser = UserBaseService::getAllByFilter([
                'email' => $account['email'],
                'vendor' => $account['vendor'],
            ]);
            $loginUser = UserBaseService::getByEmail($account['email'],[             
                'vendor' => $account['vendor'],
            ]);
            CConsole::print_line("default login user_id is ".$loginUser['user_id']);
            foreach($allUser as $user){
                if($user['user_id'] !== $loginUser['user_id']){
                    UserBaseService::update($user['user_id'],[
                        'vendor' => $user['vendor'] . date("ymdhis").mt_rand(1000,9999)
                    ]);
                }
            }            
        }
    }

    public function del_user_cache_action(){
        self::delUserCache();
    }

    public static function delUserCache(){
        UserInfoCacheService::instance()->delAll();
        UserInfoAccountCacheService::instance()->delAll();  
    }

}